<template>
  <!-- // 分配角色 -->
  <div>
    <!-- 这里将来会放置多选列表 -->
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoleIist, getUserInfo } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    curId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      // 当前有的角色
      roleIds: [],
      // 角色列表
      roleList: []
    }
  },
  // created() {
  //   this.updateRoleIist()
  // },
  methods: {
    // 取消
    closeDialog() {
      this.$emit('close')
    },
    // 确定
    async hSubmit() {
      try {
        await assignRoles({ id: this.curId, roleIds: this.roleIds })
        this.$message.success('给用户分配角色成功')
      } catch (error) {
        console.log(error)
      }
      this.$emit('close')
    },
    // 获取所有角色列表
    async updateRoleList() {
      try {
        const res = await getRoleIist({ page: 1, pagesize: 200 })
        // console.log(res)
        this.roleList = res.data.data.rows
        this.setUserInfo()
      } catch (error) {
        console.log(error)
      }
    },
    // 获取当前的角色信息
    async setUserInfo() {
      try {
        const info = await getUserInfo(this.curId)
        // console.log(info, 'info')
        this.roleIds = info.data.data.roleIds
      } catch (error) {
        console.log(error)
      }
    }

  }
}
</script>

<style>

</style>
